<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use think\Db;
use think\Request;
use think\Loader;
use app\common\logic\Capital as CapitalLogic;

/**
 * @title 资金模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class Capital extends Base
{
    /**
     * @title 资金流水
     * @description 接口说明
     * @author WCJ
     * @url /api/capital/capital_list
     * @method POST
     * @param name:start type:int require:1 default:1 other: desc:开始数
     * @param name:level type:int require:1 default:1 other: desc:团队级数
     * @param name:num type:int require:1 default:1 other: desc:数量
     * @param name:start_time type:int require:0 default:1 other: desc:开始时间
     * @param name:end_time type:int require:0 default:20 other: desc:结束时间
     * @param name:type type:int require:1 default:1 other: desc:流水类型1支出,2收入,3充值(用户),4购买股份,5会员提现,6现金支付,7充值分红,8消费分红,9推荐分红,10溢出收入11内部分红剩余,12红包提现,13股东提现,14代理金提现,15代理金收入(二级)
     *
     * @return id: 自增ID
     *
     *
     */
    public function capital_list(Request $request){

        $userid=$this->uid;
        $start=$request->post("start")?$request->post("start"):1;
        $level=$request->post("level")?$request->post("level"):0;
        $num=$request->post("num")?$request->post("num"):20;
        $type=$request->post("type");
        $start_time = $request->post("start_time");
        $end_time = $request->post("end_time");

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $userid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,  'msg' => $validate->getError()]);
            die();
        }
        if($type) {
            try {
                $tarray = json_decode($type, true);

                if (!is_array($tarray)) {
                    $ret = ['code' => 201, 'errorCode' => 404, 'msg' => '缺少重要参数'];
                    throw new ParameterException($ret);
                }
            } catch (FailException $e) {
                $ret = ['code' => 201, 'errorCode' => 404, 'msg' => '缺少重要参数'];
                throw new ParameterException($ret);
            }
        }
        if (!$validate->scene("date")->check(['date' => $start_time])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }
        if (!$validate->scene("date")->check(['date' => $end_time])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }
        $where=[];
        if($start_time){
            $start_time=strtotime($start_time);
            $where['c.addtime']=['egt',$start_time];
        }

        if($end_time){
            $end_time=strtotime($end_time);
            $where['c.addtime']=['elt',$end_time];
        }

        if($start_time&&$end_time){
       
             $where['c.addtime']=['between',[$start_time,$end_time]];
         }


        if($level){
            $where['c.level']=['eq',$level];
        }

        

        if(!$type){

            $data= Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->join('t_recharge r','c.rechange_id = r.id','LEFT')->where("c.uid",$userid)->where($where)->page($start,$num)->order("c.addtime desc")->field("c.*,s.name as shopname,r.recharge_type as rtype,r.status as rstatus")->select();
            $countss= Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->join('t_recharge r','c.rechange_id = r.id','LEFT')->where("c.uid",$userid)->where($where)->order("addtime desc")->count();

        }else{
            $where['c.type']=['in',$tarray];
            $data= Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->join('t_recharge r','c.rechange_id = r.id','LEFT')->where("c.uid",$userid)->where($where)->page($start,$num)->order("c.addtime desc")->field("c.* ,s.name as shopname,r.recharge_type as rtype,r.status as rstatus")->select();

            $countss= Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->join('t_recharge r','c.rechange_id = r.id','LEFT')->where("c.uid",$userid)->where($where)->order("c.addtime desc")->count();

        }
        $fl=  Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->where("c.uid",$userid)->where($where)->where("c.money","lt","0")->sum('c.money');
        $zl=  Db::table("t_capital_water")->alias('c') ->join('t_shop s','c.shopid = s.id','LEFT')->where("c.uid",$userid)->where($where)->where("c.money","gt","0")->sum('c.money');
        $data1=[];
        foreach ($data as $key => $value) {
            if($value['money']!=0){
                array_push($data1,$value);
            }
        }

        $datas= ["code"=>200,"msg"=>"成功","data"=>$data1,'fl'=>$fl,'zl'=>$zl,'countss'=>$countss];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }


    /**
     * @title 收益走势图页面接口
     * @description 接口说明
     * @author WCJ
     * @url /api/capital/capital_zs_list
     * @method POST
     *
     *
     * @return id: 自增ID
     *
     *
     */
    public function capital_zs_list(Request $request){

        $userid=$this->uid;


        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $userid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,  'msg' => $validate->getError()]);
            die();
        }

        $data=[];
        //累计收益
        $capital= db("capital")->where("type",2)->where("uid",$userid)->where("status",1)->find();
        $data['count_putmoney']=floor($capital['count_putmoney']*10000+$capital['bonus_money']*10000+$capital['count_packet_money']*10000)/10000;

        //当日收益
        $topday=date("Y-m-d",time());
        $start_time1= strtotime($topday);
        $end_time1= strtotime($topday." 23:59:59");
        $drwhere=[];
        $drwhere['addtime']=['between',[$start_time1,$end_time1]];
        $drwhere2=[];
        $drwhere2['bonus_time']=['between',[$topday,$topday." 23:59:59"]];
      	$data['dr_gd']= db("user_bonus_detail")->where("user_id",$userid)->where($drwhere2)->sum("bonus_money"); 
        $data['drsy']= db("capital_water")->where("uid",$userid)->where($drwhere)->where("type","in",[7,8,10])->sum("money")+$data['dr_gd'];
        $data['dr_cz']= db("capital_water")->where("uid",$userid)->where($drwhere)->where("type",7)->sum("money");
        $data['dr_xf']= db("capital_water")->where("uid",$userid)->where($drwhere)->where("type",8)->sum("money");
        $data['dr_yc']= db("capital_water")->where("uid",$userid)->where($drwhere)->where("type",10)->sum("money");
      	

        //TODO 走势图

        $yday1 =  date("Y-m-d H:i:s",strtotime('-1 days',strtotime($topday)));
        $yday2 =  date("Y-m-d H:i:s",strtotime('-2 days',strtotime($topday)));
        $yday3 =  date("Y-m-d H:i:s",strtotime('-3 days',strtotime($topday)));
        $yday4 =  date("Y-m-d H:i:s",strtotime('-4 days',strtotime($topday)));
        $yday5 =  date("Y-m-d H:i:s",strtotime('-5 days',strtotime($topday)));
        $yday6 =  date("Y-m-d H:i:s",strtotime('-6 days',strtotime($topday)));

        $yday6where=[];
        $yday6where['addtime']=['between',[strtotime($yday6),strtotime($yday5)]];
        $yday6where2['bonus_time']=['between',[$yday6,$yday5]];
        $drdata[0]['xAxis']=date("m-d",strtotime($yday6));
        $drdata[0]['yAxis']= db("capital_water")->where("uid",$userid)->where($yday6where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday6where2)->sum("bonus_money");

        $yday5where=[];
        $yday5where['addtime']=['between',[strtotime($yday5),strtotime($yday4)]];
        $yday5where2['bonus_time']=['between',[$yday5,$yday4]];
        $drdata[1]['xAxis']=date("m-d",strtotime($yday5));
        $drdata[1]['yAxis']= db("capital_water")->where("uid",$userid)->where($yday5where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday5where2)->sum("bonus_money");

        $yday4where=[];
        $yday4where['addtime']=['between',[strtotime($yday4),strtotime($yday3)]];
        $yday4where2['bonus_time']=['between',[$yday4,$yday3]];
        $drdata[2]['xAxis']=date("m-d",strtotime($yday4));
        $drdata[2]['yAxis']= db("capital_water")->where("uid",$userid)->where($yday4where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday4where2)->sum("bonus_money");

        $yday3where=[];
        $yday3where['addtime']=['between',[strtotime($yday3),strtotime($yday2)]];
        $yday3where2['bonus_time']=['between',[$yday3,$yday2]];
        $drdata[3]['xAxis']=date("m-d",strtotime($yday3));
        $drdata[3]['yAxis']= db("capital_water")->where("uid",$userid)->where($yday3where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday3where2)->sum("bonus_money");

        $yday2where=[];
        $yday2where['addtime']=['between',[strtotime($yday2),strtotime($yday1)]];
        $yday2where2['bonus_time']=['between',[$yday2,$yday1]];
        $drdata[4]['xAxis']=date("m-d",strtotime($yday2));
        $drdata[4]['yAxis']=db("capital_water")->where("uid",$userid)->where($yday2where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday2where2)->sum("bonus_money");

        $yday1where=[];
        $yday1where['addtime']=['between',[strtotime($yday1),strtotime($topday)]];
        $yday1where2['bonus_time']=['between',[$yday1,$topday]];
        $drdata[5]['xAxis']=date("m-d",strtotime($yday1));
        $drdata[5]['yAxis']=db("capital_water")->where("uid",$userid)->where($yday1where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yday1where2)->sum("bonus_money");







        //当月收益
        $firstday2 = date('Y-m-01', time());//本月第一天
        $lastday2 = date('Y-m-d', strtotime("$firstday2 +1 month -1 day"));//本月最后一天
        $start_time2= strtotime($firstday2);
        $end_time2= strtotime($lastday2);
        $dywhere=[];
        $dywhere['addtime']=['between',[$start_time2,$end_time2]];
      	$dywhere2['bonus_time']=['between',[$firstday2,$lastday2]];
      	$data['dy_gd']= db("user_bonus_detail")->where("user_id",$userid)->where($dywhere2)->sum("bonus_money"); 
        $data['dysy']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type","in",[7,8,10])->sum("money")+$data['dy_gd'];
        $data['dy_cz']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",7)->sum("money");
        $data['dy_xf']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",8)->sum("money");
        $data['dy_yc']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",10)->sum("money");
        //TODO 走势图

        $ymonth1 =  date("Y-m-d H:i:s",strtotime('-1 month',strtotime($firstday2)));
        $ymonth2 =  date("Y-m-d H:i:s",strtotime('-2 month',strtotime($firstday2)));
        $ymonth3 =  date("Y-m-d H:i:s",strtotime('-3 month',strtotime($firstday2)));
        $ymonth4 =  date("Y-m-d H:i:s",strtotime('-4 month',strtotime($firstday2)));
        $ymonth5 =  date("Y-m-d H:i:s",strtotime('-5 month',strtotime($firstday2)));
        $ymonth6 =  date("Y-m-d H:i:s",strtotime('-6 month',strtotime($firstday2)));



        $ymonth6where=[];
        $ymonth6where['addtime']=['between',[strtotime($ymonth6),strtotime($ymonth5)]];
      	$ymonth6where2['bonus_time']=['between',[$ymonth6,$ymonth5]];
        $dydata[0]['xAxis']=date("m",strtotime($ymonth6));
        $dydata[0]['yAxis']= db("capital_water")->where("uid",$userid)->where($ymonth6where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth6where2)->sum("bonus_money");


        $ymonth5where=[];
        $ymonth5where['addtime']=['between',[strtotime($ymonth5),strtotime($ymonth4)]];
      	$ymonth5where2['bonus_time']=['between',[$ymonth5,$ymonth4]];
        $dydata[1]['xAxis']=date("m",strtotime($ymonth5));
        $dydata[1]['yAxis']= db("capital_water")->where("uid",$userid)->where($yday5where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth5where2)->sum("bonus_money");


        $ymonth4where=[];
        $ymonth4where['addtime']=['between',[strtotime($ymonth4),strtotime($ymonth3)]];
      	$ymonth4where2['bonus_time']=['between',[$ymonth4,$ymonth3]];
        $dydata[2]['xAxis']=date("m",strtotime($ymonth4));
        $dydata[2]['yAxis']= db("capital_water")->where("uid",$userid)->where($ymonth4where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth4where2)->sum("bonus_money");


        $ymonth3where=[];
        $ymonth3where['addtime']=['between',[strtotime($ymonth3),strtotime($ymonth2)]];
      	$ymonth3where2['bonus_time']=['between',[$ymonth3,$ymonth2]];
        $dydata[3]['xAxis']=date("m",strtotime($ymonth3));
        $dydata[3]['yAxis']= db("capital_water")->where("uid",$userid)->where($ymonth3where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth3where2)->sum("bonus_money");

        $ymonth2where=[];
        $ymonth2where['addtime']=['between',[strtotime($ymonth2),strtotime($ymonth1)]];
     	$ymonth2where2['bonus_time']=['between',[$ymonth2,$ymonth1]];
        $dydata[4]['xAxis']=date("m",strtotime($ymonth2));
        $dydata[4]['yAxis']=db("capital_water")->where("uid",$userid)->where($ymonth2where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth2where2)->sum("bonus_money");


        $ymonth1where=[];
        $ymonth1where['addtime']=['between',[strtotime($ymonth1),strtotime($firstday2)]];
      	$ymonth1where2['bonus_time']=['between',[$ymonth1,$firstday2]];
        $dydata[5]['xAxis']=date("m",strtotime($ymonth1));
        $dydata[5]['yAxis']=db("capital_water")->where("uid",$userid)->where($ymonth1where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($ymonth1where2)->sum("bonus_money");






        //当年收益
        $firstday3 = date('Y-01-01', time());//本年第一天
        $lastday = date('Y-12-31', time())." 23:59:59";//本年最后一天
        $start_time3= strtotime($firstday3);
        $end_time3= strtotime($lastday);
        $dnwhere=[];
        $dnwhere['addtime']=['between',[$start_time3,$end_time3]];
        $dnwhere2['bonus_time']=['between',[$firstday3,$lastday]];
      	$data['dn_gd']= db("user_bonus_detail")->where("user_id",$userid)->where($dnwhere2)->sum("bonus_money"); 
        $data['dnsy']= db("capital_water")->where("uid",$userid)->where($dnwhere)->where("type","in",[7,8,10])->sum("money")+$data['dn_gd'];
        $data['dn_cz']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",7)->sum("money");
        $data['dn_xf']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",8)->sum("money");
        $data['dn_yc']= db("capital_water")->where("uid",$userid)->where($dywhere)->where("type",10)->sum("money");
        //TODO 走势图
        $yyear1 =  date("Y-m-d H:i:s",strtotime('-1 year',strtotime($firstday3)));
        $yyear2 =  date("Y-m-d H:i:s",strtotime('-2 year',strtotime($firstday3)));
        $yyear3 =  date("Y-m-d H:i:s",strtotime('-3 year',strtotime($firstday3)));
        $yyear4 =  date("Y-m-d H:i:s",strtotime('-4 year',strtotime($firstday3)));
        $yyear5 =  date("Y-m-d H:i:s",strtotime('-5 year',strtotime($firstday3)));
        $yyear6 =  date("Y-m-d H:i:s",strtotime('-6 year',strtotime($firstday3)));


        $yyear6where=[];
        $yyear6where['addtime']=['between',[strtotime($yyear6),strtotime($yyear5)]];
        $yyear6where2['bonus_time']=['between',[$yyear6,$yyear5]];
        $dndata[0]['xAxis']=date("Y",strtotime($yyear6));
        $dndata[0]['yAxis']= db("capital_water")->where("uid",$userid)->where($yyear6where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear6where2)->sum("bonus_money");

        $yyear5where=[];
        $yyear5where['addtime']=['between',[strtotime($yyear5),strtotime($yyear4)]];
        $yyear5where2['bonus_time']=['between',[$yyear5,$yyear4]];
        $dndata[1]['xAxis']=date("Y",strtotime($yyear5));
        $dndata[1]['yAxis']= db("capital_water")->where("uid",$userid)->where($yyear5where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear5where2)->sum("bonus_money");

        $yyear4where=[];
        $yyear4where['addtime']=['between',[strtotime($yyear4),strtotime($yyear3)]];
        $yyear4where2['bonus_time']=['between',[$yyear4,$yyear3]];
        $dndata[2]['xAxis']=date("Y",strtotime($yyear4));
        $dndata[2]['yAxis']= db("capital_water")->where("uid",$userid)->where($yyear4where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear4where2)->sum("bonus_money");

        $yyear3where=[];
        $yyear3where['addtime']=['between',[strtotime($yyear3),strtotime($yyear2)]];
        $yyear3where2['bonus_time']=['between',[$yyear3,$yyear2]];
        $dndata[3]['xAxis']=date("Y",strtotime($yyear3));
        $dndata[3]['yAxis']= db("capital_water")->where("uid",$userid)->where($yyear3where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear3where2)->sum("bonus_money");


        $yyear2where=[];
        $yyear2where['addtime']=['between',[strtotime($yyear2),strtotime($yyear1)]];
        $yyear2where2['bonus_time']=['between',[$yyear2,$yyear1]];
        $dndata[4]['xAxis']=date("Y",strtotime($yyear2));
        $dndata[4]['yAxis']=db("capital_water")->where("uid",$userid)->where($yyear2where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear2where2)->sum("bonus_money");


        $yyear1where=[];
        $yyear1where['addtime']=['between',[strtotime($yyear1),strtotime($firstday3)]];
        $yyear1where2['bonus_time']=['between',[$yyear1,$firstday3]];
        $dndata[5]['xAxis']=date("Y",strtotime($yyear1));
        $dndata[5]['yAxis']=db("capital_water")->where("uid",$userid)->where($yyear1where)->where("type","in",[7,8,10])->sum("money")+db("user_bonus_detail")->where("user_id",$userid)->where($yyear1where2)->sum("bonus_money");




        $datas= ["code"=>200,"msg"=>"成功","data"=>$data,"drdata"=>$drdata,"dydata"=>$dydata,"dndata"=>$dndata];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;

    }


    /**
     * @title 用户钱包
     * @description 接口说明
     * @author WCJ
     * @url /api/capital/user_capital
     * @method POST
     *
     * @return data: 数据
     *
     *
     */
    public function user_capital(Request $request){

        $userid=$this->uid;
        $user_type=$this->type;   //状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $user_type])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400, 'msg' => $validate->getError()]);
            die();
        }

        $capitallogic=new CapitalLogic();

        $data=$capitallogic->get_user_capital_data($userid,$user_type);

        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];

        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }

}